package com.share.lottery.activity.loginandre;

import static cn.smssdk.framework.utils.R.getStringRes;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import cn.smssdk.SMSSDK;

import com.share.lottery.R;
import com.share.lottery.base.BaseActivity;
import com.share.lottery.base.Constants;
import com.share.lottery.http.UsesrController;
import com.share.lottery.model.User;
import com.share.lottery.utils.StringUtil;
import com.share.lottery.view.CustomToast;
//填写从短信SDK应用后台注册得到的APPKEY
	
public class RegisterActivity extends BaseActivity {
	
	private static String APPKEY = "4a835d60372c";

	// 填写从短信SDK应用后台注册得到的APPSECRET
	private static String APPSECRET = "60cb12525cd344a468293af48b341875";

	private TextView getKey;
	private EditText  pwdEt, mobileEt, yzmEt;
	private String mobileStr;
	private String pwdStr;
	private String verificationCode;
	private boolean ready;
	private boolean isClickable=true;
	private int recLen = 60;
	private TextView tvSure;
	private TextView tvCancle;
	private String phone;
	private EditText edConfirm;//重复密码
	private String passConfirm;
	private User user;
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_reg);
		init();
		
	}
	public void findView() {
		edConfirm = (EditText) findViewById(R.id.pwd_et_confim);
		tvSure = (TextView) findViewById(R.id.tv_sure);
		tvCancle = (TextView) findViewById(R.id.tv_cancle);
		getKey = (TextView) findViewById(R.id.yzm_btn);
		mobileEt = (EditText) findViewById(R.id.mobile_et);
		pwdEt = (EditText) findViewById(R.id.pwd_et);
		yzmEt = (EditText) findViewById(R.id.yzm_et);

	}

	@Override
	public void getData() {
		user = (User) getIntent().getExtras().get("user");

	}

	private TextView titleTv;
	public void initTitle() {
		ImageButton titleLeftImg = (ImageButton) findViewById(R.id.title_left_img);
		TextView titleRightImg = (TextView) findViewById(R.id.title_right_tv);
		titleTv = (TextView) findViewById(R.id.title_tv_center);

		titleLeftImg.setImageResource(R.drawable.back);
		titleTv.setText("注册");
		titleRightImg.setVisibility(View.GONE);
		titleLeftImg.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				finish();

			}
		});
	}

	@Override
	public void setData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setonCliker() {
		getKey.setOnClickListener(this);
		tvSure.setOnClickListener(this);
		tvCancle.setOnClickListener(this);

	}
	
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_sure://提交注册
		
			submit();
			break;
		
		case R.id.yzm_btn: //获取验证码
			if (!isClickable) {
				return;
			}
			String code = "86";
			phone = mobileEt.getText().toString();
			if (StringUtil.isNullOrEmpty(phone)) {
				CustomToast.makeText(RegisterActivity.this, "请输入手机号码", 1);
				return;
			}else {
				if (StringUtil.isMobile(phone)) {
			//		SMSSDK.getVerificationCode(code, phone.trim());//发送验证码	
					UsesrController.getInstance().requestVerifacation(handler, phone);
					isClickable=false;
					recLen = 60;
					handler.postDelayed(runnable, 1000);
				}else {
					CustomToast.makeText(RegisterActivity.this, "请输入正确的手机号码", 1);
				}
			}

			break;
		case R.id.tv_cancle://取消
			RegisterActivity.this.finish();
			break;

		}

	}
	Runnable runnable = new Runnable() {

		public void run() {
			recLen--;
			getKey.setText("" + recLen + "秒" + " 重新获取验证码");
			getKey.setTextColor(Color.WHITE);
			if (recLen < 0) {
				getKey.setText("获取验证码");
				getKey.setTextColor(Color.BLACK);
				isClickable=true;
				handler.removeCallbacks(runnable);
				recLen = 60;
			}else {
				handler.postDelayed(this, 1000);
			}
			
		}
	};
	private void afterSubmit(int result, Object data) {
		Log.i("test", "提交验证码返回");
		
		if (result == SMSSDK.RESULT_COMPLETE) {
//			HashMap<String, Object> res = new HashMap<String, Object>();
//			res.put("res", true);
//			res.put("page", 2);
//			res.put("phone", data);
//			User user = new User();
//			user.setName(phone);
//			user.setPassWord(pwdStr);
//			user.setPhone(phone);
//			signForServer(user);
			
			
			
			
		//	Toast.makeText(getApplicationContext(), "验证成功，进行下一步操作", 1).show();
		} else {
			((Throwable) data).printStackTrace();
			// 验证码不正确
			int resId = getStringRes(this, "smssdk_virificaition_code_wrong");
			if (resId > 0) {
				Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
			}
		}
		
	}
	
//	Handler handler = new Handler(){
//		public void handleMessage(Message msg) {
//			int event = msg.arg1;
//			int result = msg.arg2;
//			Object data = msg.obj;
//
//			if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//				// 提交验证码返回
//				afterSubmit(result, data);
//			} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//				// 获取验证码成功后的执行动作
//				afterGet(result, data);
//			}
//
//			if (result == SMSSDK.RESULT_COMPLETE) {
//				if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//					//请求验证码后，跳转到验证码填写页面
//
//					Toast.makeText(getApplicationContext(), "获取验证码成功", 0).show();
//					
//				}
//			} else {
//				// 根据服务器返回的网络错误，给toast提示
//				try {
//					((Throwable) data).printStackTrace();
//					Throwable throwable = (Throwable) data;
//
//					JSONObject object = new JSONObject(throwable.getMessage());
//					String des = object.optString("detail");
//					if (!TextUtils.isEmpty(des)) {
//						CustomToast.makeText(getApplicationContext(), des, 0);
//
//					}
//				} catch (JSONException e) {
//					e.printStackTrace();
//				}}
//
//
//		};
//	};
	// 提交
	
	private void doMessage(Message msg) {
		switch (msg.arg1) {
		case Constants.HTTP_OK_LASTID://返回laseid


			break;

		case Constants.HTTP_OK://获取验证码成功
			Toast.makeText(getApplicationContext(), "获取验证码成功，请耐心等待", 1).show();
			break;
		case Constants.HTTP_ERROR://请求错误
			String serror = (String) msg.obj;
			showShortToast(serror);

			break;
		case Constants.HTTP_ERROR_PASER://数据解析错误
			showShortToast("数据解析错误");
			break;
		case Constants.HTTP_ERROR_NET://网络错误

		default:
			break;
		}

	}

	Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			dismissmyProgressDialog();
			dismissProgressDialog();

			switch (msg.what) {
			case Constants.VERIFACATION:
				doMessage(msg);
				break;

			default:
				break;
			}

		}


	};
	private void submit() {
		passConfirm = edConfirm.getText().toString();
		mobileStr = mobileEt.getText().toString();
		pwdStr = pwdEt.getText().toString();
		verificationCode = yzmEt.getText().toString();
		
		if (!pwdStr.equals(passConfirm)) {
			CustomToast.makeText(RegisterActivity.this, "两次输入的密码不一致", 1);
			return;
		}
		
		if (StringUtil.isNullOrEmpty(pwdStr)) {
			CustomToast.makeText(RegisterActivity.this, "请输入密码", 1);
			return;
		}
		if (StringUtil.isNullOrEmpty(verificationCode)) {
			CustomToast.makeText(RegisterActivity.this, "请输入验证码", 1);
			return;
		}
		
		if (!StringUtil.isNullOrEmpty(pwdStr) && !StringUtil.isNullOrEmpty(verificationCode)) {
			String code = "86";
			if (verificationCode == null || "".equals(verificationCode)) {
				Toast.makeText(getApplicationContext(), "请输入验证码", 0).show();
				return;
			}
			showProgreessDialog();
			if (user != null) {
				UsesrController.getInstance().register(new Handler(), mobileStr, "", "", user.getGender(), "", "", code);
			}
			
			//SMSSDK.submitVerificationCode(code, phone, verificationCode);//提交验证
		}
	}
	
	private void afterGet(int result, Object data) {
	
		
		if (result == SMSSDK.RESULT_COMPLETE) {
			int resId = getStringRes(this, "smssdk_virificaition_code_sent");
			if (resId > 0) {
				Toast.makeText(this, resId, Toast.LENGTH_SHORT).show();
			}
		}else {
			((Throwable) data).printStackTrace();
			Throwable throwable = (Throwable) data;
			// 根据服务器返回的网络错误，给toast提示
			try {
				JSONObject object = new JSONObject(throwable.getMessage());
				String des = object.optString("detail");
				if (!TextUtils.isEmpty(des)) {
					Toast.makeText(this, des, Toast.LENGTH_SHORT).show();
					return;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
	}


}
